package com.Shoping.dao.impl;

import com.Shoping.dao.ColorDao;
import com.Shoping.entity.Color;
import com.Shoping.util.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class ColorDaoImpl implements ColorDao {
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public void modifyColorByCmId(Integer cmId) throws SQLException {
        String sql = "DELETE FROM color WHERE cm_id=?;";
        queryRunner.update(DbUtils.getConnection(),sql,cmId);
    }

    @Override
    public List<Color> selectByCmId(Integer cmId) throws SQLException {
        String sql = "SELECT c_id cId,cm_id cmId,c_img cImg,c_number cNumber,c_name cName FROM color WHERE cm_id=?;";
        return queryRunner.query(DbUtils.getConnection(),sql,new BeanListHandler<>(Color.class),cmId);
    }

    @Override
    public void updateColor(Integer cId0, String cName0, Double cNumber0) throws SQLException {
        String sql = "UPDATE color SET c_number=?,c_name=? WHERE c_id=?;";
        queryRunner.update(DbUtils.getConnection(),sql,cNumber0,cName0,cId0);
    }

    @Override
    public void addColor(Integer cmId, String cName, Integer cNumber0) throws SQLException {
        String sql = "INSERT INTO color(cm_id,c_number,c_name) VALUES(?,?,?);";
        queryRunner.update(DbUtils.getConnection(),sql,cmId,cNumber0,cName);
    }
}
